package com.cloudcreativity.blyg.sqlite.utils;

import android.content.Context;

import com.cloudcreativity.blyg.sqlite.Area;
import com.cloudcreativity.blyg.sqlite.AreaDao;
import com.cloudcreativity.blyg.sqlite.City;
import com.cloudcreativity.blyg.sqlite.CityDao;
import com.cloudcreativity.blyg.sqlite.Provider;
import com.cloudcreativity.blyg.sqlite.ProviderDao;
import com.cloudcreativity.blyg.sqlite.Province;

import java.util.List;

public class AreaDaoUtils {
    private static final String TAG = AreaDaoUtils.class.getSimpleName();
    private DaoManager mManager;

    public AreaDaoUtils(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    public boolean insertProvinces(final List<Province> provinces) {
        boolean flag = false;
        try {
            mManager.getDaoSession().getProvinceDao().insertOrReplaceInTx(provinces, false);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean insertCities(List<City> cities){
        boolean flag = false;
        try {
            mManager.getDaoSession().getCityDao().insertOrReplaceInTx(cities,false);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    public boolean insertAreas(List<Area> area){
        boolean flag = false;
        try {
            mManager.getDaoSession().getAreaDao().insertOrReplaceInTx(area,false);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    public boolean isExists(){
        return mManager.getDaoSession().getProvinceDao().count()>0;
    }

    public List<Province> queryProvinces(){
        return mManager.getDaoSession().getProvinceDao().queryBuilder()
                .list();
    }

    public List<City> queryCities(String provinceId){
        return mManager.getDaoSession().getCityDao()
                .queryBuilder()
                .where(CityDao.Properties.ProvinceId.eq(provinceId))
                .list();
    }

    public List<Area> queryAreas(String cityId){
        return mManager.getDaoSession().getAreaDao()
                .queryBuilder()
                .where(AreaDao.Properties.CityId.eq(cityId))
                .list();
    }

}
